{extend name="admin@public/basic" /}

{block name="style"}
<link rel="stylesheet" href="__STATIC__/css/admin/public.css" media="all"/>
{/block}

{block name="cotent"}

<!--{if empty($module_list)}-->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
    <legend>暂无节点信息，可以先刷新节点信息 =》<button class="layui-btn layui-btn-normal refresh_node">刷新节点</button></legend>
</fieldset>
<!--{/if}-->

<div class="layui-tab layui-tab-brief" lay-filter="module">
    <ul class="layui-tab-title">
        {foreach module_list as $vo}
        <li {if $vo.is_selectd==true}class="layui-this"{/if} lay-id="{$vo.id}">{$vo.title|default=''}【{$vo.node|default=''}】</li>
        {/foreach}
    </ul>
    <div class="layui-tab-content">
        {foreach module_list as $vo}
        <div class="layui-tab-item {if $vo.is_selectd==true}layui-show{/if}">
            <table id="{$vo.node}" lay-filter="{$vo.node}"></table>
            <script type="text/html" id="checkStatus">
                <input type="checkbox" name="{{d.id}}" value="{{d.is_auth}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="modifyStatus" {{ d.is_auth== 1 ? 'checked' : '' }}>
            </script>
            <!--操作-->
            <script type="text/html" id="userListBar">

                {if auth("$thisClass/del")}
                <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                {/if}

            </script>
        </div>
        {/foreach}
    </div>
</div>
{/block}

{block name="script"}
<script>
    layui.use(['laydate', 'form', 'layer', 'table', 'laytpl', 'element'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table,
            laydate = layui.laydate,
            element = layui.element;

        /**{foreach module_list as $vo}**/
        table.render({
            elem: '#{$vo.node}',
            url: '{:url("$thisClass/index")}?module={$vo.node}',
            cellMinWidth: 95,
            height: "full-125",
            limits: [50, 100, 200, 500, 1000],
            limit: 1000,
            id: "node{$vo.node}",
            cols: [[
                {field: 'node', title: '系统节点', minWidth: 200, align: 'left', sort: true},
                {field: 'title', title: '节点名称', edit: 'text', minWidth: 200, align: "center", style: 'background-color: #009688; color: #fff;', sort: true},
                {field: 'create_at', title: '创建时间', align: 'center', sort: true},
                {field: 'is_auth', title: '节点控制', width: 150, templet: '#checkStatus', unresize: true, align: "center", sort: true},

                /**{if auth("$thisClass/del")}**/
                // {title: '操作', width: 200, templet: '#userListBar', fixed: "right", align: "center"},
                /**{/if}**/
            ]]
        });
        /**{/foreach}**/

            //Hash地址的定位
        var layid = location.hash.replace(/^#module=/, '');
        element.tabChange('module', layid);

        element.on('tab(module)', function (elem) {
            location.hash = 'module=' + $(this).attr('lay-id');
        });

        //监听启用禁用操作
        form.on('switch(modifyStatus)', function (obj) {
            console.log(this.name);
            $.get('{:url("$thisClass/status")}', {
                id: this.name  //将需要删除的newsId作为参数传入
            }, function (data) {
                console.log(data);
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1});
                } else {
                    layer.msg(data.msg, {icon: 2}, function () {
                        window.location.reload();
                    });
                }
            }).error(function () {
                layer.msg('系统错误，请稍后再试', {icon: 2}, function () {
                    tableIns.reload();
                });
            });
            return false;
        });


        //监听单元格字段编辑
        /**{foreach module_list as $vo}**/
        table.on('edit({$vo.node})', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            //弹出loading
            var index = top.layer.msg('数据修改中，请稍候', {icon: 16, time: false, shade: 0.8});
            // 实际使用时的提交信息
            $.post('{:url("$thisClass/index")}', {
                id: data.id,
                field: field,  //修改字段
                value: value,  //修改值
            }, function (res) {
                console.log(res);
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2}, function () {
                        tableIns.reload();
                    });
                }
            }).error(function () {
                layer.msg('系统错误，请稍后再试', {icon: 2}, function () {
                    tableIns.reload();
                });
            });
            return false;
        });
        /**{/foreach}**/


        //列表操作
        /**{foreach module_list as $vo}**/
        table.on('tool({$vo.node})', function (obj) {
            var layEvent = obj.event,
                data = obj.data;
            if (layEvent === 'del') { //删除
                layer.confirm('确定删除此节点？', {icon: 3, title: '提示信息'}, function (index) {
                    $.get('{:url("$thisClass/del")}', {
                        id: data.id  //将需要删除的newsId作为参数传入
                    }, function (data) {
                        console.log(data);
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1}, function () {
                                window.location.reload();
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    })
                });
            }
        });
        /**{/foreach}**/

        //刷新节点
        $(".refresh_node").click(function () {
            console.log('刷新节点！');
            $.get('{:url("$thisModule/system/refresh_node")}?type=ajax',
                function (data) {
                console.log(data);
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1}, function () {
                        window.location.reload();
                    });
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            })
        })

    });
</script>
{/block}